/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.PembayaranDAO;
import com.ivanbiz.dao.impl.PembayaranDAOImpl;
import com.ivanbiz.model.Pembayaran;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

/**
 *
 * @author IW20149X
 */
public class PembayaranTagihanLaporanService extends SelectorComposer<Component> {

    PembayaranDAO pembayaranDAO;
    NumberFormat numberFormat;
    SimpleDateFormat sdf;
    @Wire
    Listbox listPembayaran;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        pembayaranDAO = new PembayaranDAOImpl();
        numberFormat = NumberFormat.getNumberInstance();
        sdf = new SimpleDateFormat("dd MMM yyyy");
        loadPembayaran();
    }

    private void loadPembayaran() {
        try {
            listPembayaran.getItems().clear();
            int no = 0;
            int x = 0;
            List<Pembayaran> loadPembayaran = pembayaranDAO.getDataByEquals(Pembayaran.class, "status", "1");
            for (Pembayaran pembayaran : loadPembayaran) {
                no += 1;
                Listitem item = new Listitem();
                item.setValue(pembayaran);
                item.appendChild(new Listcell(String.valueOf(no)));
                item.appendChild(new Listcell(sdf.format(pembayaran.getDatePosting())));
                item.appendChild(new Listcell(pembayaran.getInvoice().getNII()));
                item.appendChild(new Listcell(pembayaran.getDebitBankAccount().getNameGL() + " A/C NO. " + pembayaran.getDebitBankAccount().getNoGL()));
                item.appendChild(new Listcell(pembayaran.getKreditBankAccount().getNameGL() + " A/C NO. " + pembayaran.getKreditBankAccount().getNoGL()));
                item.appendChild(new Listcell("Rp " + numberFormat.format(pembayaran.getJumlah()) + ",00"));
                item.appendChild(new Listcell(pembayaran.getDeskripsi()));
                item.appendChild(new Listcell("Rp " + numberFormat.format(pembayaran.getBiayaPajak()) + ",00"));
                listPembayaran.appendChild(item);
                ++x;
            }
        } catch (Exception ex) {
            Logger.getLogger(PembayaranTagihanLaporanService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Listen("onClick=#lihat;")
    public void lihat() {
        Pembayaran pembayaran = listPembayaran.getSelectedItem().getValue();
        Map map = new HashMap();
        map.put("pembayaran", pembayaran);
        Window win = (Window) Executions.createComponents("/pembayaranTagihanLaporanView.zul", null, map);
        win.doModal();
    }
}
